package com.wdzl.biz;

import com.wdzl.dao.DeptDao;
import com.wdzl.dao.EmpDao;
import com.wdzl.entity.ConditionBean;
import com.wdzl.entity.Employee;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class EmpBiz {
    private EmpDao empDao = new EmpDao();
    private DeptDao deptDao = new DeptDao();
    public List<Employee> getEmps(int page, ConditionBean condition){
        return empDao.getEmps(page,condition);
    }
    //绑定主窗体JTable的值
    public Object[][] getTableValue(List<Employee> list){
        Object[][]obj=new Object[list.size()][7];
        for(int i=0;i<list.size();i++){
            Employee eb= list.get(i);
            obj[i][0]=eb.getId();
            obj[i][1]=eb.getName();
            obj[i][2]=eb.getSex();
            obj[i][3]=eb.getDeptName();
            obj[i][4]=eb.getSalary();
            obj[i][5]=eb.getEmail();
            obj[i][6]=eb.getpNumber();
        }
        return obj;
    }
    //获取总页数
    public int getTotal(ConditionBean condition){
        return empDao.getTotal(condition);
    }
    //添加新员工
    public int addEmp(Employee emp){
        return empDao.addEmp(emp);
    }
    //删除方法（多行）
    public int delEmp(String[] ids){
        return empDao.delEmp(ids);
    }
    //修改员工信息
    public int updateEmp(Employee emp){
        return empDao.updateEmp(emp);
    }
    //导出XML
    public boolean createXmlDocument(List<Employee> list,String XMLPath) {
        try {
            XMLWriter writer = null;// 声明写XML的对象
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("UTF-8");  // 设置XML文件的编码格式 UTF-8

            File file = new File(XMLPath);//获得文件

            if (file.exists()) {
                file.delete();
            }
            // 创建xml文件
            Document document = DocumentHelper.createDocument();
            Element root = document.addElement("Emps");//添加根节点
            // Field[] properties = obj.getClass().getDeclaredFields();//获得实体类的所有属性
            //赋值
            for (int i=0;i<list.size();i++) {
                Element element = root.addElement("Emp");            //二级节点
                Employee user=(Employee)list.get(i);
                element.addElement("id").setText(user.getId()+"");
                element.addElement("name").setText(user.getName());
                element.addElement("sex").setText(user.getSex());
                element.addElement("depart").setText(user.getDeptName());
                element.addElement("salary").setText(user.getSalary()+"");
                element.addElement("email").setText(user.getEmail()+"");
                element.addElement("pNumber").setText(user.getpNumber()+"");
            }
            writer = new XMLWriter(new FileOutputStream(file), format);
            writer.write(document);
            writer.close();
            return true;
        } catch (Exception e) {
            System.out.println("XML文件写入失败");
            e.printStackTrace();
            return false;
        }
    }
    //XML文件的导入
    public List parserXml(File f) {
        List list=new ArrayList();
        try {
            SAXReader reader = new SAXReader();
            Document doc = reader.read(f);
            Element root = doc.getRootElement();

            Element foo;

            for (Iterator i = root.elementIterator("Emp"); i.hasNext();){
                foo = (Element)i.next();
                String id=foo.elementText("id");
                String name=foo.elementText("name");
                String sex=foo.elementText("sex");

                String depart=foo.elementText("depart");

                String salary=foo.elementText("salary");
                String email=foo.elementText("email");
                String pNumber=foo.elementText("pNumber");

                Employee e=new Employee();
                try {
                    e.setId(Integer.parseInt(id));
                    e.setName(name);
                    e.setSex(sex);
                    e.setDeptName(depart);
                    e.setSalary(Double.parseDouble(salary));
                    e.setEmail(email);
                    e.setpNumber(pNumber);
                } catch (Exception e2) {
                    return null;
                }
                list.add(e);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    //同步数据库
    public boolean sysnchrDB(List<Employee> list){
        for(int i=0;i<list.size();i++){
            Employee eb=(Employee)list.get(i);
            int id=eb.getId();
            String name=eb.getName();
            String sex=eb.getSex();
            if(!sex.equals("男")&&!sex.equals("女")){
                return false;
            }
            String dname=eb.getDeptName();
            int did = deptDao.getDid(dname);
            if(did==0){
                return false;
            }
            double salary=eb.getSalary();
            String email=eb.getEmail();
            String pNumber=eb.getpNumber();
            Employee emp=new Employee(id, name, sex, did, salary,email,pNumber);
            if(this.getEmp(id)){
                this.updateEmp(emp);
            }else{
                this.addEmp(emp);
            }
        }
        return true;
    }

    //根据id查询员工
    public boolean getEmp(int id){
        return empDao.getEmp(id);
    }
}
